<?php
namespace Dft\Controllers;

use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Secret\Common\Utils;

class PageCSP implements ControllerProviderInterface {

	public function connect(Application $app) {
		$content = array();
		$controllers = $app['controllers_factory'];
		$controllers->match('/', array($this,'first'), 'GET');
		$controllers->match('/', array($this,'page'), 'GET');
		$controllers->match('/{urlFragment}/', array($this,'page'), 'GET');
		$controllers->match('/{urlFragment}/{param1}/', array($this,'page'), 'GET');
		$controllers->match('/{urlFragment}/{param1}/{param2}/', array($this,'page'), 'GET');
		$controllers->match('/{urlFragment}/{param1}/{param2}/{param3}/', array($this,'page'), 'GET');
		return $controllers;
	}

	public function first(Application $app, Request $request) {
		return $this->page($app, $request, 'home');
	}
	
	public function page(Application $app, Request $request, $urlFragment = '', $param1 = null, $param2 = null, $param3 = null) {
		global $CMS_COLUMN_META;
		$repo = $app['em']->getRepository('Secret\Entities\Content');
		$model = $repo->model('Page');
		
		$urlFragment = $urlFragment && !empty($urlFragment) ? $urlFragment : 'home';
// 		Utils::dump($urlFragment);exit;
		$current = $repo->data($model, 'entity.urlFragment = :v1', array('v1' => urldecode($urlFragment)), array());
		if (count($current['_data']) == 0) {
			$current = $repo->data($model, 'entity.slug = :v1', array('v1' => urldecode($urlFragment)), array());
		}
		if (count($current['_data']) == 0) {
			$current = $repo->data($model, 'entity.title = :v1', array('v1' => urldecode($urlFragment)), array());
		}
		if (count($current['_data']) == 0) {
			$current = $repo->data($model, 'entity.id = :v1', array('v1' => urldecode($urlFragment)), array());
		}
		if (count($current['_data']) == 0) {
			$app->abort(404);
		}
// 		var_dump($current['_data'][0]['template']);exit;

		$nav = array();
		$categoryModel = $repo->model('Page category');
		$categories = $repo->data($categoryModel);
		foreach ($categories['_data'] as $category) {
			$id = $request->get('id');
			$result = $repo->data('Page', '', array(), array());
			$pages = array();
			foreach ($result['_data'] as $itm) {
				$itm['catRank'] = ((empty($itm['catRank']) || !$itm['catRank'])) ? array() : (array)json_decode($itm['catRank']);
				$itm['catParent'] = ((empty($itm['catParent']) || !$itm['catParent'])) ? array() : (array)json_decode($itm['catParent']);
				$itm['category'] = ((empty($itm['category']) || !$itm['category'])) ? array() : (array)json_decode($itm['category']);
				
				if (!in_array($category['id'], $itm['category']) || $itm['menu'] == 1) {
					continue;
				}
					
				$itm['rank'] = isset($itm['catRank']['cat' . $category['id']]) ? $itm['catRank']['cat' . $category['id']] : 0;
				$itm['parentId'] = isset($itm['catParent']['cat' . $category['id']]) ? $itm['catParent']['cat' . $category['id']] : 0;
				if ($category['id'] == -1 && count($itm['category']) == 0) {
					$pages[] = $itm;
				} else if (in_array($category['id'], $itm['category'])){
					$pages[] = $itm;
				}
			}
			
			usort($pages, function($a, $b) {
				return $a["rank"] > $b["rank"] ? 1 : -1;
			});
			
			$nav[$category['code']] = Utils::buildTree(array('id' => 0), $pages);
		}
		
// 		Utils::dump($nav);exit;
		
		return $app['twig']->render($current['_data'][0]['template'], array(
			'page' =>$current['_data'][0],
			'nav' => $nav,
			'urlFragment' => $urlFragment,
			'id' => $id,
			'param1' => $param1,
			'param2' => $param2,
			'param3' => $param3,
		));
	}
}